package com.tms.pidan.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Slf4j
@Configuration
@EnableSwagger2
public class ConfigurationSupport extends WebMvcConfigurationSupport {


    /**
     * deserializerByType 解决string类型入参转为 LocalDateTime 格式问题
     * todo：有问题暂时注释掉
     *
     * @return
     */
    //@Bean
    //public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
    //    return builder -> builder
    //            .deserializerByType(Enum.class, EnumDeserializer.INSTANCE)
    //            .deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DateUtils.DEFAULT_DATE_TIME_FORMAT)))
    //            .deserializerByType(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DateUtils.DEFAULT_DATE_FORMAT)))
    //            .deserializerByType(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DateUtils.DEFAULT_TIME_FORMAT)));
    //}
    @Bean
    public Docket createRestApi() {
        // 文档类型
        return new Docket(DocumentationType.SWAGGER_2)
                // 创建api的基本信息
                .apiInfo(apiInfo())
                // 选择哪些接口去暴露
                .select()
                // 扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.tms.pidan.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("皮蛋物流管理数据聚合服务--Swagger文档")
                .version("1.0")
                .build();
    }

    /**
     * 防止@EnableMvc把默认的静态资源路径覆盖了，手动设置的方式
     *
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 解决静态资源无法访问
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        // 解决swagger无法访问
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

    }

    /**
     * 解决序列化
     * todo：有问题暂时注释掉，此方法调用了下方的内部类
     */
    //@Override
    //public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    //    log.info("初始化jackson配置( 枚举类型、时间格式 )");
    //    //在json转换之前先进行string转换
    //    converters.add(new StringHttpMessageConverter());
    //    //添加json转换
    //    MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
    //    jackson2HttpMessageConverter.setObjectMapper(new JacksonObjectMapper());
    //    converters.add(jackson2HttpMessageConverter);
    //    //追加默认转换器
    //    super.addDefaultHttpMessageConverters(converters);
    //}

    /**
     * todo：有问题暂时注释掉
     */
//    class JacksonObjectMapper extends ObjectMapper {
//        public JacksonObjectMapper() {
//            super();
//            //收到未知属性时不报异常
//            this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
//
//            //反序列化时，属性不存在的兼容处理
//            this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
//
//            SimpleModule simpleModule = new SimpleModule()
//                    .addSerializer(BigInteger.class, ToStringSerializer.instance)
//                    .addSerializer(BigDecimal.class, new BigDecimalSerializer())
//                    .addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DateUtils.DEFAULT_DATE_TIME_FORMAT)))
//                    .addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DateUtils.DEFAULT_DATE_FORMAT)))
//                    .addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DateUtils.DEFAULT_TIME_FORMAT)))
//                    .addSerializer(Date.class, new DateSerializer(false, new SimpleDateFormat(DateUtils.DEFAULT_DATE_TIME_FORMAT)));
//
//            this.registerModule(simpleModule);
//            //处理空指针时设置的值
////            this.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
////                @Override
////                public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
////                    String fieldName = gen.getOutputContext().getCurrentName();
////                    try {
////                        //反射获取字段类型
////                        Field field = gen.getCurrentValue().getClass().getDeclaredField(fieldName);
////                        if (Objects.equals(field.getType(), String.class)) {
////                            //字符串型空值""
////                            gen.writeString("");
////                            return;
////                        } else if (Objects.equals(field.getType(), List.class)) {
////                            //列表型空值返回[]
////                            gen.writeStartArray();
////                            gen.writeEndArray();
////                            return;
////                        } else if (Objects.equals(field.getType(), Map.class)) {
////                            //map型空值返回{}
////                            gen.writeStartObject();
////                            gen.writeEndObject();
////                            return;
////                        }
////                    } catch (NoSuchFieldException e) {
////                    }
////                    //默认返回""
////                    gen.writeString("");
////                }
////            });
//        }
//    }
}
